package com.topsoft.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

@Component
public class AppContext implements ApplicationContextAware {

	/**
	 * 上下文对象实例
	 */
	public static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext value) throws BeansException {
		applicationContext = value;
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public static Object getBean(String name){
		return getApplicationContext().getBean(name);
	}

	public static <T> T getBean(Class<T> clazz){
		return getApplicationContext().getBean(clazz);
	}

	public static <T> T getBean(String name,Class<T> clazz){
		return getApplicationContext().getBean(name, clazz);
	}

	/**
	 * 纯算法生成全局唯一 流水号
	 * @return
	 */
	//线程安全的 AtomicInteger
	private static AtomicInteger atomicInteger = new AtomicInteger(0);
	public static String getKeyId(){
		atomicInteger.getAndIncrement();
		if(atomicInteger.get()>999)atomicInteger.set(1);
		String keyId= DateUtils.getDateByHHmmssSSS().replaceAll("-","").replaceAll(" ","").replaceAll(":","");
		keyId=keyId.substring(2,keyId.length())+"R"
				+MiscHelper.fillStr(String.valueOf((int)(Math.random()*999)), 3, "0")
				+"S"+MiscHelper.fillStr(atomicInteger.get()+"", 3, "0");
		try { Thread.sleep(1); } catch (Exception e) {}
//        String keyID=Misc.getDate().toString().replaceAll("-","").substring(2)+System.nanoTime()+""+Misc.fillStr(count+"", 3, "0");//getKeyID();
		if(keyId.length()>26) {
			return keyId.substring(0, 26);
		}else {
			return keyId;
		}
	}

	public static org.springframework.core.env.Environment environment(){
		return getBean(org.springframework.core.env.Environment.class);
	}


	static Logger logger = LoggerFactory.getLogger("");

	public static void initStart(long start, ApplicationContext value) {
		applicationContext = value;

		long end = System.currentTimeMillis();

		org.springframework.core.env.Environment environment = environment();

		logger.info("------------------------------------------------------------------------");
		logger.info("启动完毕 耗时:" +(end - start)/1000+"秒");
		logger.info("server.port               : {}{}", environment.getProperty("server.port")," (服务端口)");
		logger.info("spring.profiles.active    : {}", environment.getProperty("spring.profiles.active"));
		logger.info("spring.redis.host         : {}", environment.getProperty("spring.redis.host"));
		logger.info("spring.redis.port         : {}", environment.getProperty("spring.redis.port"));
		logger.info("spring.redis.database     : {}", environment.getProperty("spring.redis.database"));

		logger.info("spring.datasource.database: {}{}", environment.getProperty("spring.datasource.database")," (连接的数据库名)");
		logger.info("spring.datasource.driver  : {}", environment.getProperty("spring.datasource.driver-class-name"));

//		String database = environment.getProperty("spring.datasource.database");
//		database = database!=null?database:environment.getProperty("spring.datasource.database");
//		database = database!=null?database:environment.getProperty("spring.datasource.hikari.database");
//		if (database != null) {
//			int pos = database.indexOf("?");
//			logger.info("spring.datasource.url     : {}", pos > 0 ? database.substring(0, pos) : database + " ...");
//		}
//
//		String url = environment.getProperty("spring.datasource.url");
//		url = url!=null?url:environment.getProperty("spring.datasource.jdbc-url");
//		url = url!=null?url:environment.getProperty("spring.datasource.hikari.jdbc-url");
//		if (url != null) {
//			int pos = url.indexOf("?");
//			logger.info("spring.datasource.url     : {}", pos > 0 ? url.substring(0, pos) : url + " ...");
//		}

		logger.info("------------------------------------------------------------------------");
	}
}